package ch8;

import java.net.Socket;
import java.net.UnknownHostException;
import java.io.*;

public class FileGet {

	public static void main(String[] args) {
		Socket s = null;
		FileOutputStream r = null;
		InputStream in = null;
		try {
			s = new Socket("192.168.10.93", 6666);
			in = s.getInputStream();
			// System.out.println(in.available() + ">>>>");
			int len = in.read();// 后面多少个字节是文件名
			byte[] bname = new byte[len];
			in.read(bname);
			String str = new String(bname);
			r = new FileOutputStream("e:/" + str);
			byte[] b = new byte[10240];
			int l = 0;
			while ((l = in.read(b)) != -1) {
				r.write(b, 0, l);
			}

		} catch (UnknownHostException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} finally {
			if (r != null) {
				try {
					r.close();
				} catch (IOException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
			}
			if (s != null) {
				try {
					s.close();
				} catch (IOException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
				try {
					in.close();
				} catch (IOException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}

			}
		}

	}

}
